﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Web.Blog.Services.Menus;

namespace Web.Blog.Controllers
{
    [Route("blog/[controller]")]
    [ApiController]
    public class MenuController : BlogControllerBase
    {
        private readonly IMemoryCache _memoryCache;
        public MenuController(IMemoryCache memoryCache)
        {
            _memoryCache = memoryCache;
        }
        [HttpGet]
        public ActionResult List()
        {
            if (_memoryCache.TryGetValue("menu", out IEnumerable<MenuChildren> value))
            {
                return Ok(value);
            }
            else
            {
                var query = BDC.BlogMenus.Where(c => c.IsVisble).OrderByDescending(c => c.Sort);
                var menus = ObjectMapper.Map<List<MenuDto>>(query);
                var childer = MenuFinder.GetBlogMenuChildren(menus);
                _memoryCache.Set("menu", childer, TimeSpan.FromSeconds(10));
                return Ok(childer);
            }

        }
    }
}